home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / tde40.zip / findrep.c < prev    next >
C/C++ Source or Header  |  1994-06-05  |  52KB  |  1,712 lines

  1. /*******************  start of original comments  ********************/
  2. /*
  3.  * Written by Douglas Thomson (1989/1990)
  4.  *
  5.  * This source code is released into the public domain.
  6.  */
  7.  
  8. /*
  9.  * Name:    dte - Doug's Text Editor program - find/replace module
  10.  * Purpose: This file contains the functions relating to finding text
  11.  *           and replacing text.
  12.  *          It also contains the code for moving the cursor to various
  13.  *           other positions in the file.
  14.  * File:    findrep.c
  15.  * Author:  Douglas Thomson
  16.  * System:  this file is intended to be system-independent
  17.  * Date:    October 1, 1989
  18.  */
  19. /*********************  end of original comments   ********************/
  20.  
  21. /*
  22.  * The search and replace routines have been EXTENSIVELY rewritten.  The
  23.  * "brute force" search algorithm has been replaced by the Boyer-Moore
  24.  * string search algorithm.  This search algorithm really speeds up search
  25.  * and replace operations.
  26.  *
  27.  *                  Sketch of the BM pattern matching algorithm:
  28.  *
  29.  *    while (not found) {
  30.  *       fast skip loop;    <===  does most of the work and should be FAST
  31.  *       slow match loop;   <===  standard "brute force" string compare
  32.  *       if (found) then
  33.  *          break;
  34.  *       shift function;    <===  mismatch, shift and go to fast skip loop
  35.  *    }
  36.  *
  37.  * See:
  38.  *
  39.  *   Robert S. Boyer and J Strother Moore, "A fast string searching
  40.  *     algorithm."  _Communications of the ACM_ 20 (No. 10): 762-772, 1977.
  41.  *
  42.  * See also:
  43.  *
  44.  *   Zvi Galil, "On Improving the Worst Case Running Time of the Boyer-
  45.  *    Moore String Matching Algorithm."  _Communications of the ACM_
  46.  *    22 (No. 9): 505-508, 1979.
  47.  *
  48.  *   R. Nigel Horspool, "Practical fast searching in strings." _Software-
  49.  *    Practice and Experience_  10 (No. 3): 501-506, 1980.
  50.  *
  51.  *   Alberto Apostolico and Raffaele Giancarlo, "The Boyer-Moore-Galil
  52.  *    String Searching Strategies Revisited."  _SIAM Journal on Computing_
  53.  *    15 (No. 1): 98-105, 1986.
  54.  *
  55.  *   Andrew Hume and Daniel Sunday, "Fast String Searching."  _Software-
  56.  *    Practice and Experience_ 21 (No. 11): 1221-1248, November 1991.
  57.  *
  58.  *   Timo Raita, "Tuning the Boyer-Moore-Horspool String Searching Algorithm."
  59.  *    _Software-Practice and Experience_ 22 (No. 10): 879-884, October 1992.
  60.  *
  61.  *
  62.  *                            Boyer-Moore in TDE
  63.  *
  64.  * Hume and Sunday demonstrated in their paper that using a simple shift
  65.  * function after a mismatch gives one of the fastest search times with the
  66.  * Boyer-Moore algorithm.  When searching normal text for small patterns
  67.  * (patterns less than 30 characters or so), Hume and Sunday found that the
  68.  * faster the algorithm can get back into the fast skip loop with the
  69.  * largest shift value then the faster the search.  Some algorithms can
  70.  * generate a large shift value, but they can't get back into the skip loop
  71.  * very fast.  Hume and Sunday give a simple method for computing a shift
  72.  * constant that, more often than not, is equal to the length of the search
  73.  * pattern.  They refer to the constant as mini-Sunday delta2 or md2.  From
  74.  * the end of the string, md2 is the first leftmost occurrence of the
  75.  * rightmost character.  Hume and Sunday also found that using a simple string
  76.  * compare as the match function gave better performance than using the C
  77.  * library memcmp( ) function.  The average number of compares in the slow
  78.  * loop was slightly above 1.  Calling the memcmp( ) function to compare 1
  79.  * character slows down the algorithm.  See the Hume and Sunday paper for an
  80.  * excellent discussion on fast string searching.  Incidentally, Hume and
  81.  * Sunday describe an even faster, tuned Boyer-Moore search function.
  82.  *
  83.  * The Boyer-Moore search algorithm in TDE was rearranged so that it is now
  84.  * almost identical to the original version Boyer-Moore described in CACM.
  85.  * The simple shift function in TDE was replaced by the mini-delta2 shift
  86.  * function in the Hume and Sunday paper.
  87.  *
  88.  * I am not very fond of WordStar/TURBO x style search and replace prompting,
  89.  * which is used in the DTE 5.1 editor.  Once the search pattern has been
  90.  * defined, one only needs to press a key to search forwards or backwards.
  91.  * The forward or backward search key may be pressed at any time in any file
  92.  * once the pattern has been entered.  Also, the search case may be toggled
  93.  * at any time in any file once the pattern has been entered.
  94.  *
  95.  * Thanks to Tom Waters, twaters@relay.nswc.navy.mil, for finding a bug when
  96.  * building the ignore skip index array in TDE 1.3 and previous versions.
  97.  * BTW, I added assertions to those functions that build the skip index
  98.  * array to make certain that everything is everything.
  99.  *
  100.  * Thanks also to David Merrill, u09606@uicvm.uic.edu, for recommending a
  101.  * change and the source code for the solution to what can be an annoying
  102.  * feature in the find function.  Pressing <FindForward> before defining
  103.  * the pattern would cause TDE to display an error message.  Since we already
  104.  * know that the search pattern is undefined, we can easily prompt for
  105.  * the search pattern.  I usually tolerate such features until I get tired
  106.  * of being annoyed with error messages and finally write a solution.
  107.  * Dave, thanks for the solution and the easy-to-implement code.  Although
  108.  * such changes are small, they really make for a more comfortable editor.
  109.  *
  110.  * New editor name:  TDE, the Thomson-Davis Editor.
  111.  * Author:           Frank Davis
  112.  * Date:             June 5, 1991, version 1.0
  113.  * Date:             July 29, 1991, version 1.1
  114.  * Date:             October 5, 1991, version 1.2
  115.  * Date:             January 20, 1992, version 1.3
  116.  * Date:             February 17, 1992, version 1.4
  117.  * Date:             April 1, 1992, version 1.5
  118.  * Date:             June 5, 1992, version 2.0
  119.  * Date:             October 31, 1992, version 2.1
  120.  * Date:             April 1, 1993, version 2.2
  121.  * Date:             June 5, 1993, version 3.0
  122.  * Date:             August 29, 1993, version 3.1
  123.  * Date:             November 13, 1993, version 3.2
  124.  * Date:             June 5, 1994, version 4.0
  125.  *
  126.  * This modification of Douglas Thomson's code is released into the
  127.  * public domain, Frank Davis.   You may distribute it freely.
  128.  */
  129.  
  130. #include "tdestr.h"
  131. #include "common.h"
  132. #include "tdefunc.h"
  133. #include "define.h"
  134.  
  135.  
  136. /*
  137.  * Name:     get_replacement_flags
  138.  * Purpose:  To input find and replace flags.
  139.  * Date:     June 5, 1991
  140.  * Modified: November 13, 1993, Frank Davis per Byrial Jensen
  141.  * Passed:   line:  prompt line
  142.  * Returns:  OK if flags were entered, ERROR if user wanted to abort
  143.  */
  144. int  get_replacement_flags( int line )
  145. {
  146. register int c;
  147. int  func;
  148. int  rc;
  149. #if defined( __UNIX__ )
  150.  chtype display_buff[MAX_COLS+2];       /* chtype is defined in curses.h */
  151. #else
  152.  char display_buff[(MAX_COLS+2)*2];
  153. #endif
  154.  
  155.    save_screen_line( 0, line, display_buff );
  156.    eol_clear( 0, line, g_display.text_color );
  157.    /*
  158.     * options: prompt or no prompt (p/n)?
  159.     */
  160.    s_output( find1, line, 0, g_display.message_color );
  161.    xygoto( strlen( find1 ) + 2, line );
  162.    do {
  163.       c = getanswerkey( );
  164.       func = getfunc( c );
  165.    } while (c != L_PROMPT  &&  c != L_NO_PROMPT  &&
  166.             c != RTURN  &&  c != ESC  &&  func != AbortCommand);
  167.    restore_screen_line( 0, line, display_buff );
  168.    switch (c) {
  169.       case L_PROMPT :
  170.       case RTURN :
  171.          g_status.replace_flag = PROMPT;
  172.          rc = OK;
  173.          break;
  174.       case L_NO_PROMPT :
  175.          g_status.replace_flag = NOPROMPT;
  176.          rc = OK;
  177.          break;
  178.       default :
  179.          rc = ERROR;
  180.    }
  181.    bm.search_defined = rc;
  182.    return( rc );
  183. }
  184.  
  185.  
  186. /*
  187.  * Name:     ask_replace
  188.  * Purpose:  Ask user if he wants to (r)place, (s)kip, or (e)xit.
  189.  * Date:     June 5, 1991
  190.  * Modified: November 13, 1993, Frank Davis per Byrial Jensen
  191.  * Returns:  TRUE if user wants to replace, ERROR otherwise.
  192.  * Passed:   window:   pointer to current window
  193.  *           finished: TRUE if user pressed ESC or (e)xit.
  194.  */
  195. int  ask_replace( TDE_WIN *window, int *finished )
  196. {
  197. regist